/*
 * @Date: 2023-12-19 14:50:05
 * @LastEditors: lyx
 * @LastEditTime: 2024-02-23 16:27:13
 * @FilePath: \taro-template3\src\components\MsgContent\index.tsx
 * @Description:
 */
import { View } from "@tarojs/components";
import styles from "./index.module.scss";
import React, { useContext, useMemo } from "react";
import { ChatContext, } from "@/pages/index/hooks/store";
import cName from "classnames";
import useMsgContent from "./hooks/useMsgContent";
import MsgActionPup from "../MsgActionPup";
import ChatItem from "../ChatItem";
import { IQItem } from "@/pages/index/hooks/type";
import "highlight.js/styles/vs2015.css";
import { CHAT_ROLE } from "@/utils/chat";
import { AI_ACTION_LIST, USER_ACTION_LIST } from "../MsgActionPup/hooks/store";

function MsgContent() {
  const { qList } = useContext(ChatContext);

  const msgContentInst = useMsgContent();

  const mode = useMemo(() => {
    if (msgContentInst.pupData) {
      return msgContentInst?.pupData?.item?.messageBody?.contents?.[0]?.role === CHAT_ROLE.assistant ? 'large' : 'mini'
    }
    return

  }, [msgContentInst.pupData]);

  const actionList = useMemo(() => {
    if (msgContentInst.pupData.item) {
      const item = msgContentInst.pupData.item;
      if (item?.messageBody?.contents?.[0]?.role === CHAT_ROLE.assistant) {
        if (!item.parentMsgId) {
          // ! 自动生成的 不支持重新生成
          return AI_ACTION_LIST.filter((item) => item.key !== "reload");
        } else {
          return AI_ACTION_LIST;
        }
      } else {
        return USER_ACTION_LIST;
      }
    } else {
      return [];
    }

  }, [msgContentInst.pupData]);

  return (
    <View id="chat_box" className={cName(styles["chat_box"])}>
      {qList.map((item: IQItem, index: number) =>
        <ChatItem index={index} key={item.id} chatItem={item} handleLongPress={msgContentInst.handleLongPress} />
      )}


      <MsgActionPup
        mode={mode}
        offset={{
          direction:'left',
          distance:'4rem', // 左边有头像等 需要偏移
        }}
        pupData={msgContentInst.pupData}
        actionList={actionList}
        handleClick={msgContentInst.handleClick}
      />
    </View>
  );
}

export default MsgContent;
